﻿
Public Class FormMain
    Implements Widgets.IXboxInfoRetrevalView
    Implements Widgets.ITwitterUpdateView

    Private m_ExitCall As Boolean = False

#Region " XboxInfoRetreval & IXboxInfoRetrevalView "

    Private WithEvents XboxInfoRetreval As New Widgets.XboxInfoRetreval(Of FormMain)(Me)

    Private Sub XboxInfoRetreval_Tick(ByVal source As Widgets.XboxInfoRetreval(Of FormMain), ByVal e As Widgets.XboxInfoRetrevalEventArgs) Handles XboxInfoRetreval.Tick
        Dim display As New System.Text.StringBuilder
        Debug.Write("XboxInfoRetreval_Tick")
        '------------------
        display.AppendLine("Xbox Live to Twitter")
        If Not FormSettings.CheckSettings() Then
            display.AppendLine("You must wait untill the data has been retrieved at least once!")
        ElseIf Not My.Settings.Enabled Then
            display.AppendLine("Currently DISABLED. Check the settings...")
        ElseIf e.LastUpdate Is Nothing Then
            If e.TimeSinceLastCheck > e.TimeBetweenChecks Then
                display.AppendFormat("Next: Now{0}", vbCrLf)
            Else
                display.AppendFormat("Next: {0}(s){1}", CInt(e.TimeBetweenChecks.Subtract(e.TimeSinceLastCheck).TotalSeconds), vbCrLf)
            End If
            display.AppendLine("Waiting for first update...")
        Else
            If e.TimeSinceLastCheck > e.TimeBetweenChecks Then
                display.AppendFormat("Next: Now{0}", vbCrLf)
            Else
                display.AppendFormat("Next: {0}(s){1}", CInt(e.TimeBetweenChecks.Subtract(e.TimeSinceLastCheck).TotalSeconds), vbCrLf)
            End If
        End If
        If display.ToString.Length > 63 Then
            Me.ux_NotifyIcon.Text = display.ToString.Substring(0, 63)
        Else
            Me.ux_NotifyIcon.Text = display.ToString
        End If
    End Sub

    Private Sub XboxInfoRetreval_Update(ByVal source As Widgets.XboxInfoRetreval(Of FormMain), ByVal e As Widgets.XboxInfoRetrevalEventArgs) Handles XboxInfoRetreval.Update
        Debug.Write("XboxInfoRetreval_Update")
        Dim tipTitle As String = ""
        Dim tipContent As String = ""
        If e.LastUpdate.PresenceInfo.Online Then
            tipTitle = "You are Online"
            tipContent = e.LastUpdate.ToPresenceString
            TwitterUpdate.Send(e.LastUpdate.PresenceInfo.Info)
            Me.ux_NotifyIcon.ShowBalloonTip(2000, tipTitle, tipContent, ToolTipIcon.None)
        Else
            tipTitle = "You are Offline"
            tipContent = e.LastUpdate.ToPresenceString
            Me.ux_NotifyIcon.ShowBalloonTip(2000, tipTitle, tipContent, ToolTipIcon.None)
        End If

    End Sub

    Public Function GetGamerTag() As String Implements Widgets.IXboxInfoRetrevalView.GetGamerTag
        If Not My.Settings.GamerTag.Length > 3 Then
            GetSettings()
        End If
        Return My.Settings.GamerTag
    End Function

    Public Function CheckSetting() As Boolean Implements Widgets.IXboxInfoRetrevalView.CheckSetting
        Return FormSettings.CheckSettings
    End Function

#End Region

#Region " TwitterUpdate & ITwitterUpdateView "

    Private WithEvents TwitterUpdate As New Widgets.TwitterUpdate(Of FormMain)(Me)

    Public Function GetTwiterCredentials() As Object Implements Widgets.ITwitterUpdateView.GetTwiterCredentials
        If Not My.Settings.TwitterEmail.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterPassword.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterUsername.Length > 3 Then
            GetSettings()
        End If
        Return New With { _
                    .Email = My.Settings.TwitterEmail, _
                    .Password = My.Settings.TwitterPassword, _
                    .Username = My.Settings.TwitterUsername _
                        }
    End Function

#End Region

#Region " Form Events "

    Private Sub ux_ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_ExitToolStripMenuItem.Click
        m_ExitCall = True
        Me.Close()
    End Sub

    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not e.CloseReason = CloseReason.WindowsShutDown Or Not m_ExitCall Then
            e.Cancel = True
        End If
    End Sub

    Private Sub ux_CheckNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_CheckNowToolStripMenuItem.Click
        XboxInfoRetreval.CheckXboxLiveStatus()
    End Sub

    Private Sub ux_DisplayLiveDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_DisplayLiveDataToolStripMenuItem.Click
        If XboxInfoRetreval.LastUpdate Is Nothing Then
            MsgBox("You must wait untill the data has been retrieved at least once!")
            Exit Sub
        End If
        FormDisplay.ShowXboxLiveStatus(XboxInfoRetreval.LastUpdate)
    End Sub

    Private Sub ux_SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_SettingsToolStripMenuItem.Click
        GetSettings()
    End Sub

    Private Sub FormMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If FormSettings.CheckSettings() = False Then
            Me.ux_NotifyIcon.ShowBalloonTip(10000, My.Application.Info.Title, "You must enter your settings before your Twitter can be updated!", ToolTipIcon.Warning)
        End If
    End Sub

#End Region

    Private Sub GetSettings()
        If FormSettings.ShowSettings = Windows.Forms.DialogResult.OK Then
            FormSettings.CheckSettings()
        End If
    End Sub

    Private Sub ux_NotifyIcon_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ux_NotifyIcon.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Try
                Dim tipTitle As String = ""
                Dim tipContent As String = ""
                If XboxInfoRetreval.LastUpdate.PresenceInfo.Online Then
                    tipTitle = "You are Online"
                    tipContent = XboxInfoRetreval.LastUpdate.ToPresenceString
                Else
                    tipTitle = "You are Offline"
                    tipContent = XboxInfoRetreval.LastUpdate.ToPresenceString
                End If
                Me.ux_NotifyIcon.ShowBalloonTip(2000, tipTitle, tipContent, ToolTipIcon.None)
            Catch ex As Exception

            End Try
        End If
    End Sub

End Class